# Maintainer: Maciej Suminski <maciej.suminski@cern.ch>
# Contributor: Unai Martinez-Corral <unai.martinezcorral@ehu.eus>
#
# Based on Arch ngspice PKGBUILD:
# https://git.archlinux.org/svntogit/community.git/tree/trunk/PKGBUILD?h=packages/ngspice
#   Contributor: Kyle Keen <keenerd@gmail.com>
#   Contributor: Abhishek Dasgupta <abhidg@gmail.com>
#   Contributor: Jason Taylor <jftaylor21@gmail.com>
#   Contributor: Luis Henrique <lmello.009@gmail.com>

_realname=ngspice
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=35
pkgrel=2
pkgdesc="Mixed-level/Mixed-signal circuit simulator based on Spice3f5, Cider1b1, and Xspice (mingw-w64)"
url='https://ngspice.sourceforge.io/'
license=('BSD')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
depends=(
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "${MINGW_PACKAGE_PREFIX}-cc"
)
source=(
  "https://downloads.sourceforge.net/project/${_realname}/ng-spice-rework/${pkgver}/${_realname}-${pkgver}.tar.gz"
  "https://downloads.sourceforge.net/project/${_realname}/ng-spice-rework/${pkgver}/${_realname}-doc-${pkgver}.tar.gz"
  "no-explicit-lstdc++.patch"
)
sha256sums=(
  'c1b7f5c276db579acb3f0a7afb64afdeb4362289a6cab502d4ca302d6e5279ec'
  'aef6cde1939ac2396d7f90ccfee7bc540fddc7abe605d497495bc0d93404386f'
  '26c34504e11ee753855da3b6d5ff44f0247dbcedb9eb6fe5efcf6f50f2927c97'
)

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/no-explicit-lstdc++.patch"
  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-shared-${CARCH}" ]] && rm -rf "${srcdir}/build-shared-${CARCH}"
  mkdir -p "${srcdir}/build-shared-${CARCH}" && cd "${srcdir}/build-shared-${CARCH}"

  # FS#45230, create so lib
  # shared lib sets flags and modifies headers, needs dedicated pass
  # adding --with-readline disables libngspice-0.dll
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    LDFLAGS+=" -lomp"
  fi
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-debug \
    --enable-openmp \
    --enable-xspice \
    --enable-cider \
    --with-ngshared
  make

  cd "${srcdir}/${_realname}-${pkgver}"
  [[ -d "${srcdir}/build-static-${CARCH}" ]] && rm -rf "${srcdir}/build-static-${CARCH}"
  mkdir -p "${srcdir}/build-static-${CARCH}" && cd "${srcdir}/build-static-${CARCH}"

  # gcc is being weird
  LDFLAGS+=" -lgdi32"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-debug \
    --enable-xspice \
    --enable-cider \
    --enable-openmp \
    --with-wingui
  make
}

package() {
  cd "${srcdir}/build-static-${CARCH}"
  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/build-shared-${CARCH}"
  make DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
